/**
 * @author caojj
 * @descript 常用的输入相关逻辑
 * @version 1.0.0.4
 */

try{XN}catch(e){XN={}}

/**
 * 可输入字数
 * @param {Object} charMaxCount
 * @param {Object} regFormat
 * @param {Object} tipInfo
 * @param {Object} tipInfoCot
 * @param {Object} input
 */
XN.remainInputCount = function(charMaxCount, regFormat, tipInfo, tipInfoCot, input) {
	var ipt = input?input:this;
//	var ipt = document.createElement("inpute");
	
	function judgeCharCount(ev) {
		var me = ipt;	//this;
		var flag = true;
		
		var val = me.value;
		var evt = ev || window.event;
		
		if (val.length >= charMaxCount) {			
				
//			var filterList = "16|18|36|35|9|37|39|8|46";		//过滤的按键	shift|alt|home|end|tab|left|right|backSpace|Delete
			var filterList = "16|18|36|35|9|37|38|39|40|8|46"
			var evtKeyCode = evt.keyCode;
//			alert(evtKeyCode);
			var reg = "/\\b"+ evtKeyCode +"\\b/";
			
			if (filterList.match(eval(reg))) {
				flag = true;
			} else {				
				flag = false;
				me.value = val.substr(0, charMaxCount);
			}
			
		} else {
			flag = true;
		}
		
		function countCharRemain() {
			var strLen = 0;
			if (me.value&&me.defaultValue&&me.value==me.defaultValue) {
				strLen = 0;
			} else {
				strLen = me.value.length
			}
			var len = charMaxCount-strLen>0?charMaxCount-strLen:0;
			tipInfoCot.innerHTML = tipInfo.replace("$1", len);			
		}		
		
//		this.value = val.substr(0, charMaxCount);
		setTimeout(countCharRemain, 0);
		
		return flag;
	}

	ipt.onkeydown = judgeCharCount;
//	ipt.onkeyup = judgeCharCount;
//	ipt.onkeypress = judgeCharCount;
//	ipt.oncontextmenu = judgeCharCount;
	ipt.onchange = judgeCharCount;
	ipt.onpaste = judgeCharCount;
	
	ipt.clear = function() {
		this.value = "";
		judgeCharCount();
		if (this.getAttribute("defaultValue")) {
			this.value = this.getAttribute("defaultValue");
		}
	}
	
	
	tipInfoCot.innerHTML = tipInfo.replace("$1", charMaxCount)
}

/**
 * 输入框默认提示
 * @param {Object} ipt
 * @param {Object} info
 */
XN.inputDefaultTip = function(ipt, info) {
	ipt.defaultValue = info;
	ipt.value = info;
	ipt.onfocus = function() {
		if (this.value.replace(/^\s*|\s*$/g, "")==this.defaultValue) {
			this.value = "";
		}
	}
	ipt.onblur = function(){
		if (this.value.replace(/^\s*|\s*$/g, "") == "") {
			this.value = this.defaultValue;
		}
	}
}

/**
 * 验证码
 * @param {Object} url
 * @param {Object} ipt
 * @param {Object} img
 */
XN.enableVerifyCode = function(url, ipt, img, link, subFun) {
	if(!img) {
		img = this;
	}
	
	var imgFlag = img.tagName.toUpperCase() == "IMG" || (img.type && img.type == "image");
	if (imgFlag) {
		img.src = url;
	} else {
		img.style.backgroundImage = "url(" + url + ")";
	}
	
	if (!img.title) {
		img.title = "点击更换";
	}
	
	img.onclick = function(ev) {
		var _url = url+ '&r=' + new Date().getTime();
		
		if (imgFlag) {
			this.src = _url;	// this.src.replace(/&r=.*/, '') + '&r=' + new Date().getTime();
		} else {
			this.style.backgroundImage = "url("+_url+")";
		}		
		img.blur();
		if (ipt) {
			try {
				if (ev||window.event) {
					ipt.focus();
					ipt.select();						
				}
			} catch(ex) {}			
		}
	}
	
	if (link) {
		link.onclick = function(){
			setTimeout(function() {img.onclick();},100);
		}	
	}
	
	
//	img.onclick();
	setTimeout(function() {
		img.onclick();
	},10);
	
	if (subFun) {
		ipt.onkeydown = function(ev) {
			var evt = ev || window.event;
			if (evt.keyCode=='13') {
				subFun.call();
			}
		}
	}
}

/**
 * 自适应图片
 * @param {Object} elem
 */
XN.fitImgSize = function(elem) {	
	if(!elem||elem.target) {
		elem = _$("photoViewer");
	}
	elem.style.width = "auto";
	elem.style.height = "auto";
	var w = elem.width;
	var h = elem.height;
	var cotW = elem.parentNode.offsetWidth;
	var cotH = elem.parentNode.offsetHeight;
	
	if (FWK.isIE6) {		
		elem.style.width = "";
		elem.style.height = "";
		w = elem.width;
		h = elem.height;
		
//		cotW = Number($("picContent").currentStyle.width.replace("px",""));
//		cotH = Number($("picContent").currentStyle.height.replace("px",""));
//		
		setTimeout(reSizeImg ,100);
	}else{
		reSizeImg();
	}
	
	function reSizeImg() {
		var scale = cotH / cotW;
		if(!scale){scale=0.75}
//		alert(w+"\n"+cotH+"\n"+cotW)
		if(w*scale>h) {	//w
			if(w>cotW) {
				elem.style.width = cotW + "px";
			}else{
				elem.style.width = "auto";
			}
			//设置垂直居中
//			alert(t)
			var level = w / cotW;
			var t = level>1 ? (cotH - h / level) / 2 : (cotH - h)/2;
//			elem.style.top = t + "px";
			elem.style.marginTop = t + "px";
		}else{	//h
			if(h>cotH) {
				elem.style.height = cotH + "px";
			}else{
				elem.style.width = "auto";
			}
			var level = h / cotH;
			var t = level>1 ? (cotH - h / level) / 2 : (cotH - h)/2;
//			elem.style.top = t + "px";
			elem.style.marginTop = t + "px";
		}
		
	}
}
